package cn.virens.http.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.http.server.annotation.RequestPath;

import cn.hutool.core.convert.Convert;
import cn.virens.App;
import cn.virens.app.common.BytesBuilder;
import cn.virens.common.StringUtil;
import cn.virens.http.ResultText;

@RequestPath("/device")
public class CommandController {
	protected final static Logger logger = LoggerFactory.getLogger(CommandController.class);

	//开关设备设置
	@RequestPath(value = "/off.json")
	public HttpResponse off(HttpRequest request) throws Exception {
		String imei = request.getParam("mac");
		String type = "FF";
		String acStatus = "FF";
		String deviceStatus = request.getParam("deviceStatus");
		logger.debug("发送({})控制信息命令：{}", imei, deviceStatus);

		return ResultText.of(request, () -> {
			String onOrOff = "";
			if (StringUtil.equals(deviceStatus, "off")) onOrOff = "07";
			if (StringUtil.equals(deviceStatus, "on")) onOrOff = "08";
			logger.debug("本次开关机指令为--------------->" + onOrOff);
			return App.device().deviceSwitch(imei, type, acStatus, onOrOff);
		});
	}

	/**
	 * 激活命令(也可以进行设置参数：流量、天数)
	 */
	@RequestPath(value = "/surplus.json")
	public HttpResponse activation(HttpRequest request) throws Exception {
		String imei = request.getParam("mac");
		BytesBuilder builder = new BytesBuilder();
		builder.append(getInt(request, "leaseType", 0xff), 1);//租赁模式
		builder.append(getInt(request, "activationType", 0xff), 1);//激活状态
		builder.append(getInt(request, "deviceType", 0xff), 1);//水机状态
		builder.append(getInt(request, "surplusFlow", 0xffff), 2);//剩余流量
		builder.append(getInt(request, "surplusDays", 0xffff), 2);//剩余天数
		builder.append(getInt(request, "accumulativeFlow", 0xffff), 2);//累计流量
		builder.append(getInt(request, "accumulativeDays", 0xffff), 2);//累计天数
		builder.append(getInt(request, "pumpAccumulativeDays", 0xffffffff), 4);//泵累计天数
		builder.append((int) (System.currentTimeMillis() / 1000), 4);//当前时间
		builder.append(getInt(request, "filterType", 0xff), 1);//滤芯模式
		builder.append(getInt(request, "filterNumber", 0xff), 1);//滤芯条数
		builder.append(getInt(request, "filterSurplusFlow", 0xffffffff), 4);//滤芯1剩余流量
		builder.append(getInt(request, "filterSurplusDays", 0xffffffff), 4);//滤芯1剩余天数
		builder.append(getInt(request, "otherFilterSurplusFlow", 0xffffffff), 4);//其余滤芯剩余流量
		builder.append(getInt(request, "otherFilterSurplusDays", 0xffffffff), 4);//其余滤芯剩余天数

		return ResultText.of(request, () -> {
			logger.debug("<---------------开始发送设置指令()--------------->");
			return App.device().activation(imei, builder.toArray());
		});
	}

	//设备数据查询
	@RequestPath(value = "/query.json")
	public HttpResponse query(HttpRequest request) throws Exception {
		String imei = request.getParam("mac");
		logger.debug("手动发送查询指令：{}");

		return ResultText.of(request, () -> {
			return App.device().queryData(imei);
		});
	}

	private static Integer getInt(HttpRequest request, String key, Integer def) {
		return Convert.toInt(request.getAttribute(key), def);
	}
}
